package com.util;

import sun.misc.BASE64Encoder;

import java.util.UUID;

/**
 * 产生大写加小写、短横线、下划线的22位不重复id
 */
public class UUIDBase64Util {
    /**
     *调用这个，产生id
     */
    public static String generate() {
        UUID uuid = UUID.randomUUID();
        byte[] uuidArr = asByteArray(uuid);
        String s = new BASE64Encoder().encode(uuidArr);
        return s.replace("=", "").replace("/", "_").replace("+", "-");
    }

    private static byte[] asByteArray(UUID uuid) {
        long msb = uuid.getMostSignificantBits();
        long lsb = uuid.getLeastSignificantBits();
        byte[] buffer = new byte[16];

        for (int i = 0; i < 8; i++) {
            buffer[i] = (byte) (msb >>> 8 * (7 - i));
        }
        for (int i = 8; i < 16; i++) {
            buffer[i] = (byte) (lsb >>> 8 * (7 - i));
        }
        return buffer;
    }

    public static void main(String[] args) {
        System.out.println(generate());
    }
}
